#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target)
    {
        vector<vector<int>> ret;
        sort(nums.begin(), nums.end());

        int n = nums.size();
        for (int i = 0; i < n; )
        {
            for (int j = i + 1; j < n; )
            {
                int left = j + 1, right = n - 1;
                long long aim = (long long)target - nums[i] - nums[j];
                while (left < right)
                {
                    int sum = nums[left] + nums[right];
                    if (sum > aim)
                        --right;
                    else if (sum < aim)
                        ++left;
                    else
                    {
                        ret.push_back({ nums[i],nums[j],nums[left++],nums[right--] });
                        while (left < right && nums[left] == nums[left - 1])
                            ++left;
                        while (left < right && nums[right] == nums[right + 1])
                            --right;
                    }
                }
                ++j;
                while (j < n && nums[j] == nums[j - 1])
                    ++j;
            }
            ++i;
            while (i < n && nums[i] == nums[i - 1])
                ++i;
        }
        return ret;
    }
};

int main()
{
    vector<int> nums({ 1, 0, -1, 0, -2, 2 });
    vector<vector<int>> ret = Solution().fourSum(nums, 0);
    for (auto& e : ret)
    {
        for (auto& a : e)
        {
            cout << a << " ";
        }
        cout << endl;
    }

    return 0;
}

class Solution2 {
public:
    int LastRemaining_Solution(int n, int m)
    {
        list<int> child;
        for (int i = 0; i < n; ++i)
        {
            child.emplace_back(i);
        }

        auto it = child.begin();
        while (child.size() > 1)
        {
            int t = m;
            while (--t)
            {
                ++it;
                if (it == child.end())
                    it = child.begin();
            }
            it = child.erase(it);
            if (it == child.end())
                it = child.begin();
        }

        return child.back();
    }
};

//int main()
//{
//    cout << Solution().LastRemaining_Solution(5, 3) << endl;
//
//    return 0;
//}

class Solution1 {
public:
    int nextTime(vector<vector<int>>& grid, vector<vector<int>>& next_grid, int& time)
    {
        for (int i = 0; i < grid.size(); ++i)
        {
            for (int j = 0; j < grid[0].size(); ++j)
            {
                if (grid[i][j] == 2)
                {
                    if (j + 1 < grid[0].size() && grid[i][j + 1] != 0)
                    {
                        next_grid[i][j + 1] = 2;
                    }
                    if (j - 1 >= 0 && grid[i][j - 1] != 0)
                    {
                        next_grid[i][j - 1] = 2;
                    }
                    if (i + 1 < grid.size() && grid[i + 1][j] != 0)
                    {
                        next_grid[i + 1][j] = 2;
                    }
                    if (i - 1 >= 0 && grid[i - 1][j] != 0)
                    {
                        next_grid[i - 1][j] = 2;
                    }
                }
            }
        }
        bool flag1 = false;
        for (auto& e : next_grid)
        {
            for (auto& n : e)
            {
                if (n == 1)
                {
                    flag1 = true;
                    break;
                }
            }
        }

        if (flag1 && grid == next_grid)
            return -1;
        if (grid == next_grid)
            return time;
        ++time;
        grid = next_grid;

        return nextTime(grid, next_grid, time) == -1 ? -1 : time;
    }

    int rotApple(vector<vector<int> >& grid)
    {
        vector<vector<int>> next_grid(grid);
        int time = 0;

        return nextTime(grid, next_grid, time);
    }
};

//int main()
//{
//    vector<vector<int>> nums({ {2,1,1},{1,0,1},{1,1,1} });
//    cout << Solution().rotApple(nums) << endl;
//
//    return 0;
//}

int main1()
{
    vector<vector<int>> nums;
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        int a, b, c;
        cin >> a >> b >> c;
        nums.push_back({ a,b,c });
    }

    vector<int> ans(n, 0);
    for (int i = 0; i < n; ++i)
    {
        if (nums[i][0] >= 1 && nums[i][1] >= 1 && nums[i][2] >= 1)
        {
            int flag = min(nums[i][0], nums[i][1]);
            flag = min(flag, nums[i][2]);
            ans[i] += 2 * flag;
            nums[i][0] -= flag, nums[i][1] -= flag, nums[i][2] -= flag;
        }
        if (nums[i][1] >= 2)
        {
            ans[i] += nums[i][1] - 1;
        }
    }

    for (auto& e : ans)
    {
        cout << e << endl;
    }

    return 0;
}